package com.linsir.gen.service.impl;

import com.linsir.core.service.impl.BaseServiceImpl;
import com.linsir.gen.dto.SysGenTableDTO;
import com.linsir.gen.entity.SysGenTable;
import com.linsir.gen.entity.SysGenTableColumn;
import com.linsir.gen.mapper.SysGenTableMapper;
import com.linsir.gen.service.SysGenTableService;
import com.linsir.gen.vo.SysGenTableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * @author ：linsir
 * @date ：Created in 2022/10/7 23:22
 * @description：
 * @modified By：
 * @version:
 */

@Service
public class SysGenTableServiceImpl extends BaseServiceImpl<SysGenTableMapper,SysGenTable> implements SysGenTableService {

    @Autowired
    private SysGenTableMapper sysGenTableMapper;

   /***
   * @Description:   drop table
   * @Param: [sysGenTableDTO]
   * @return: int
   * @Author: linsir
   * @Date: 2022/10/11 12:44
   */
    @Override
    public int dropTable(SysGenTableDTO sysGenTableDTO) {
        StringBuffer sql = new StringBuffer();
        sql.append((new StringBuilder("drop table if exists ")).append(sysGenTableDTO.getName()).append(" ;").toString());
        return sysGenTableMapper.doSql(sql.toString());
    }

    /***
    * @Description: 创建table
    * @Param: [sysGenTableVO]
    * @return: int
    * @Author: linsir
    * @Date: 2022/10/11 12:46
    */
    public int createTable(SysGenTableDTO sysGenTableDTO)
    {
        StringBuffer sql = new StringBuffer();
        List<SysGenTableColumn> getTableColumnList = sysGenTableDTO.getColumnList();
        sql.append((new StringBuilder("create table ")).append(sysGenTableDTO.getName()).append(" (").toString());
        String pk = "";

        for(Iterator iterator = getTableColumnList.iterator(); iterator.hasNext();)
        {
            SysGenTableColumn column = (SysGenTableColumn)iterator.next();
            if(column.isPk())
            {
                sql.append((new StringBuilder("  ")).append(column.getName()).append(" ").append(column.getJdbcType()).append(" comment '").append(column.getComments()).append("',").toString());
                pk = (new StringBuilder(String.valueOf(pk))).append(column.getName()).append(",").toString();
            } else
            {
                sql.append((new StringBuilder("  ")).append(column.getName()).append(" ").append(column.getJdbcType()).append(" comment '").append(column.getComments()).append("',").toString());
            }
        }

        sql.append("primary key (" + pk.substring(0, pk.length() - 1) + ") ");
        sql.append(") comment '" + sysGenTableDTO.getComments() + "'");
        return sysGenTableMapper.doSql(sql.toString());
    }



    @Override
    public boolean synchDb(Long sysGenTableId) {
        boolean result = false;
        SysGenTableDTO sysGenTableDTO = getViewObject(sysGenTableId,SysGenTableDTO.class);
        dropTable(sysGenTableDTO);
        if (createTable(sysGenTableDTO)>0)
        {
            result = true;
        }
       return result;
    }
}
